/**
 * The MIT License (MIT)
 * Copyright (c) 2020 铭软科技(mingsoft.net)
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


package net.mingsoft.cms.action.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateNotFoundException;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.cms.biz.ContentShuffleService;
import net.mingsoft.cms.entity.ContentShuffleTitleEntity;
import net.mingsoft.cms.util.CmsParserUtil.URLUtil;
import net.mingsoft.mdiy.util.ParserUtil;

/**
 * 动态生成页面
 *
 */
@Controller("dynamicArtAction")
public class DynamicArtAction extends net.mingsoft.cms.action.BaseAction {

    
    @Autowired
    private ContentShuffleService shuffleService;




    @Value("${ms.diy.html-dir:html}")
    private String htmlDir;
    
    @Value("${ms.diy.artimg-path:artimg}")
    private String imgpath;

    @RequestMapping("/dmArea.do")
    @ResponseBody
    public String dmArea(String are,String area,HttpServletRequest req, HttpServletResponse resp) {
    	Integer count = 10+(int)Math.random()*10;//抽取数量
    	
    	Map map = BasicUtil.assemblyRequestMap();
        map.forEach((k, v) -> {
            map.put(k, v.toString().replaceAll("('|\"|\\\\)", "\\\\$1"));
        });
        //动态解析
        //map.put(ParserUtil.IS_DO, true);
        //设置动态请求的模块路径
        map.put(ParserUtil.MODEL_NAME, "mcms");
        map.put(ParserUtil.HTML,htmlDir);
        //动态数据
        if(are==null) are="";
        //查询地区
        String result = new String(Base64.getDecoder().decode(are));
        String areaa = new String(Base64.getDecoder().decode(area));
        map.put("areaTag",result);
        map.put("area",areaa);
        map.put("currentDate",new Date());
        map.put("urlEncoder",  new URLUtil());
        //解析后的内容
        String content = "";
        try {
            //根据模板路径，参数生成
            content = ParserUtil.rendering("dynamicAreaIndex" + ParserUtil.HTM_SUFFIX, map);
        } catch (TemplateNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedTemplateNameException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }
    
    @RequestMapping("/dmcart.do")
    @ResponseBody
    public String dynamicArticle(String aid,HttpServletRequest req, HttpServletResponse resp) {
        Map map = BasicUtil.assemblyRequestMap();
        map.forEach((k, v) -> {
            map.put(k, v.toString().replaceAll("('|\"|\\\\)", "\\\\$1"));
        });
       // map.put(ParserUtil.URL, BasicUtil.getUrl());
        //动态解析
        //map.put(ParserUtil.IS_DO, true);
        //设置动态请求的模块路径
        map.put(ParserUtil.MODEL_NAME, "mcms");
        map.put(ParserUtil.HTML,htmlDir);
        //动态数据
        if(aid==null) aid="";
        ContentShuffleTitleEntity title = shuffleService.getTitle(aid);
        map.put("art_title", title);
        map.put("art_head", shuffleService.getRandomHead(title.getCatId()));
        int counts = 5+(int)Math.random()*10;
        map.put("art_bodys", shuffleService.getRandomBodys(counts,title.getCatId()));
        map.put("art_foot", shuffleService.getRandomFoot(title.getCatId()));
        map.put("art_imgs", shuffleService.getRandomImgs(3));
        map.put("art_time", new Date());
        map.put("art_imgpath", imgpath);
        map.put("art_lasttitle", shuffleService.getRandomTitle());
        map.put("art_nexttitle", shuffleService.getRandomTitle());
        map.put("art_hits", shuffleService.getHits());
        map.put("art_orthers", shuffleService.getRandomTitles(10));
        //解析后的内容
        String content = "";
        try {
            //根据模板路径，参数生成
            content = ParserUtil.rendering("dynamicArticle" + ParserUtil.HTM_SUFFIX, map);
        } catch (TemplateNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedTemplateNameException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }
    @RequestMapping("/dmcall.do")
    @ResponseBody
    public String dynamicArticleAll(String aid,HttpServletRequest req, HttpServletResponse resp) {
        Map map = BasicUtil.assemblyRequestMap();
        map.forEach((k, v) -> {
            map.put(k, v.toString().replaceAll("('|\"|\\\\)", "\\\\$1"));
        });
       // map.put(ParserUtil.URL, BasicUtil.getUrl());
        //动态解析
        //map.put(ParserUtil.IS_DO, true);
        //设置动态请求的模块路径
        map.put(ParserUtil.MODEL_NAME, "mcms");
        map.put(ParserUtil.HTML,htmlDir);
        //动态数据
        if(aid==null) aid="";
        ContentShuffleTitleEntity title = shuffleService.getTitle(aid);
        map.put("art_title",title);
        List<String> bodys = new ArrayList();
        bodys.add(shuffleService.getRandomHead(title.getCatId()));
        bodys.add(shuffleService.getRandomFoot(title.getCatId()));
        int counts = 5+(int)Math.random()*10;
        bodys.addAll(shuffleService.getRandomBodys(counts,title.getCatId()));
        map.put("art_imgs", shuffleService.getRandomImgs(3));
        map.put("art_time", new Date());
        map.put("art_imgpath", imgpath);
        map.put("art_lasttitle", shuffleService.getRandomTitle());
        map.put("art_nexttitle", shuffleService.getRandomTitle());
        map.put("art_hits", shuffleService.getHits());
        map.put("art_orthers", shuffleService.getRandomTitles(10));
        Collections.shuffle(bodys);
        map.put("art_bodys", bodys);
        //解析后的内容
        String content = "";
        try {
            //根据模板路径，参数生成
            content = ParserUtil.rendering("dynamicArticle2" + ParserUtil.HTM_SUFFIX, map);
        } catch (TemplateNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedTemplateNameException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }
}
